package fc.testmain;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSON;
import fc.FcHttpCon;
import fc.entity.OaEntity;
import weaver.conn.RecordSet;
import weaver.upgradetool.wscheck.Util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class HJ_CREATE {
    public static void main(String[] args) {


        OaEntity oe=new OaEntity();

        oe.setREQUESTID("20250410181300000003");

        oe.setTYPENO("YHJ01");//单据类别

        oe.setTYPENO("YJH01");//单据类别

        oe.setJUDGE1("N");//用来判断

        oe.setUSERID("MW17071100690");//人员
        oe.setDEPARTMENTID("1000034");//部门
        oe.setCUS_NO("S07039");//供应商编号
        oe.setCUR_ID("CNY");//币别
        oe.setTAX_NO("T08");//税种
        oe.setFKTJ("A20");//付款条件
        oe.setJYTJ("CFR");//交易条件
        oe.setSXRQ("2025/04/10");//生效日期

        //String requestid = info.getRequestid();
        // String tablename = info.getRequestManager().getBillTableName();
/*
        String requestid ="1213776";
        String tablename = "formtable_main_722";
        oe.setREQUESTID(requestid);



        String sql="select requestid,hjdjb typeno,sqry  USERID ,sqbm DEPARTMENTID,gys CUS_NO,bz CUR_ID,FKTJ,JYTJ,SXRQ,sz TAX_NO from  "+tablename+"  where requestid="+requestid;
        RecordSet rs = new RecordSet();
        rs.executeSql(sql);
        if(rs.next()){
            oe.setREQUESTID(Util.null2String(rs.getString("REQUESTID")));
            oe.setTYPENO(Util.null2String(rs.getString("typeno")));
            oe.setUSERID(Util.null2String(rs.getString("USERID")));
            oe.setDEPARTMENTID(Util.null2String(rs.getString("DEPARTMENTID")));
            oe.setCUS_NO(Util.null2String(rs.getString("CUS_NO")));
            oe.setCUR_ID(Util.null2String(rs.getString("CUR_ID")));
            oe.setFKTJ(Util.null2String(rs.getString("FKTJ")));
            oe.setJYTJ(Util.null2String(rs.getString("JYTJ")));
            oe.setSXRQ(Util.null2String(rs.getString("SXRQ")));
            oe.setTAX_NO(Util.null2String(rs.getString("TAX_NO")));

            if(oe.getTYPENO().equals("YHJ01")){
                oe.setJUDGE1("N");//用来判断
            }else{
                oe.setJUDGE1("Y");//用来判断
            }
        }


        String sql1="select  o1.lh LJBH,o1.dw DW,o2.bz CUR_ID,o2.sz TAX_NO,o1.dj UP,o1.bz   from "+tablename+"_dt1  o1,"+tablename+" o2 where o1.mainid=o2.id  and o2.requestid="+requestid;

        RecordSet rs1 = new RecordSet();
        rs1.executeSql(sql1);
        while(rs1.next()){
            List<OaEntity> dts=oe.getDT1();
            OaEntity dt1=new OaEntity();
            dt1.setLJBH(Util.null2String(rs1.getString("LJBH")));
            dt1.setDW(Util.null2String(rs1.getString("DW")));
            dt1.setCUR_ID(Util.null2String(rs1.getString("CUR_ID")));
            dt1.setTAX_NO(Util.null2String(rs1.getString("TAX_NO")));
            dt1.setUP(Util.null2String(rs1.getString("UP")));
            dt1.setREM(Util.null2String(rs1.getString("REM")));
            dts.add(dt1);
            oe.setDT1(dts);
        }

 */
        List<OaEntity> dts=oe.getDT1();
        OaEntity dt1=new OaEntity();
        dt1=new OaEntity();
        dt1.setLJBH("121007002920");
        dt1.setDW("PCS");
        dt1.setCUR_ID("CNY");
        dt1.setTAX_NO("T08");
        dt1.setUP("11111");
        dt1.setREM("BEIZHU1");
        dt1.setCAUSE("CAUSE1");
        dts.add(dt1);
        oe.setDT1(dts);

        dts=oe.getDT1();
        dt1=new OaEntity();
        dt1.setLJBH("11XPMD50AAA02");
        dt1.setDW("PCS");
        dt1.setCUR_ID("CNY");
        dt1.setTAX_NO("T08");
        dt1.setUP("11111");
        dt1.setREM("BEIZHU2");
        dt1.setCAUSE("CAUSE2");

        dts.add(dt1);
        oe.setDT1(dts);

        String testparam="";


        for(int i=0;i<oe.getDT1().size();i++){

            if (!"".equals(testparam)){
                testparam=testparam+",";
            }
             testparam+=
                    "{"+
                            "\"seq_no\": \""+(i+1)+"\",  "+//序号，同一单不可以相同，必填
                            "\"item_no\": \""+oe.getDT1().get(i).getLJBH()+"\",  "+//料号，必填
                            "\"item_feature_no\": \" \","+
                            "\"series_no\": \"\","+
                            "\"item_category\": \"\","+
                            "\"vendor_item_no\": \"\","+
                            "\"program_job_no\": \"\","+
                            "\"op_seq\": \"\","+
                            "\"process_no\": \"\","+
                            "\"reason_code\": \"\","+
                            "\"unit\": \""+oe.getDT1().get(i).getDW()+"\",   "+//物料单位，必填
                            "\"currency_no\": \""+oe.getDT1().get(i).getCUR_ID()+"\","+//，必填
                            "\"tax_no\": \""+oe.getDT1().get(i).getTAX_NO()+"\",  "+//税种，必填
                            "\"reason_prc_increase\": \""+oe.getDT1().get(i).getCAUSE()+"\",  "+//涨价原因，当当前价格高于以往，要在这里填原因

                            "\"remark\": \""+oe.getDT1().get(i).getREM()+"\","+
                            "\"price\": \""+oe.getDT1().get(i).getUP()+"\",  "+//单价，必填
                            "\"is_qty_valuation\": \"N\",  "+ //是否分量计，必填（若这里为Y，则下面的分量明细必填，否则分量计价明细里面可以为空）
                            "\"pricing_valuation_detail\": ["+
                            "]"+
                            "}";

        }




        DateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String  timestamp= format.format(new Date());
        DateFormat formataa = new SimpleDateFormat("yyyy-MM-dd");
        String createdate = formataa.format(new Date());
        String creater=oe.getUSERID();
        String bumen=oe.getDEPARTMENTID();


        oe.setREQUESTID(timestamp);

        String json_param=
                "{"+
                        "\"key\": \"a3b67072afa24b1e5d15e6486dd83bc8\","+
                        "\"type\": \"sync\","+
                        "\"host\": {"+
                        "\"prod\": \"OAPI\","+
                        "\"ip\": \"10.100.96.96\","+
                        "\"lang\": \"zh_CN\","+
                        "\"timestamp\": \""+timestamp+"\","+
                        "\"acct\": \"tiptop\""+
                        "},"+
                        "\"service\": {"+
                        "\"prod\": \"T100\","+
                        "\"name\": \"oapi.po.pricing.crt\","+
                        "\"ip\": \"10.100.96.96\","+
                        "\"id\": \"topprod\""+
                        "},"+
                        "\"datakey\": {"+
                        "\"EntId\": \"23\","+
                        "\"CompanyId\": \"ZH00\","+
                        "\"doc_type_no\": \""+oe.getTYPENO()+"\",   "+ //YJH01是标准采购核价，YHJ02是委的， YHJ03是委外返修的，必填
                        "\"item_no\": \"\""+
                        "},"+
                        "\"payload\": {"+
                        "\"std_data\": {"+
                        "\"parameter\": {"+
                        "\"enterprise_no\": \"23\", "+//必给，与上面ENTID一致
                        "\"site_no\": \"ZH00\","+
                        "\"is_digiwin_product\": \"Y\","+
                        "\"pricing_list_master\": ["+
                        "{"+
                        "\"source_no\": \""+oe.getREQUESTID()+"\",   "+//来源单号， 每次提次不可以相同，必填
                        "\"doc_type_no\": \""+oe.getTYPENO()+"\", "+ //必填，与上面一致
                        "\"outsourcing_type\": \""+oe.getJUDGE1()+"\",   "+//是否是委外，必填
                        "\"doc_date\": \""+createdate+"\",  "+//必填
                        "\"applicant_no\": \""+creater+"\",  "+ //申请人，必填
                        "\"application_department\": \""+bumen+"\",   "+//申请人所在部门编号，必填
                        "\"supplier\": \""+oe.getCUS_NO()+"\",  "+ //供应商编号，必填
                        "\"currency_no\": \""+oe.getCUR_ID()+"\","+//，必填
                        "\"tax_no\": \""+oe.getTAX_NO()+"\",   "+//税种，必填
                        "\"payment_condition\": \""+oe.getFKTJ()+"\",   "+//付款条件，必填
                        "\"trade_condition\": \""+oe.getJYTJ()+"\",  "+//交易条件，必填
                        "\"approval_content\": \"1\","+
                        "\"effective_date\": \""+oe.getSXRQ()+"\", "+  //生效开始日，必填
                        "\"expiration_date\": \"\","+
                        "\"price_valuation_target_control\": \"1\","+
                        "\"price_valuation_control\": \"1\","+
                        "\"is_qty_valuation\": \"N\",  "+//分量计价否
                        "\"remark\": \"主要备注//\","+
                        "\"modify_no\": \""+creater+"\","+
                        "\"create_date\": \""+createdate+"\","+
                        "\"doc_approve_set\": \"N\","+
                        "\"pricing_list_detail\": ["+

                        testparam+



                        "]"+
                        "}"+
                        "]"+
                        "}"+
                        "}"+
                        "}"+
                        "}";

        String data = HttpRequest.post(FcHttpCon.ZH_YFSY_XQ).body(json_param).execute().body();

        System.out.println(data);

        String result="ERROR";
        if(!"".equals(testparam)){
            try {
                Object succesResponse = JSON.parse(data);
                Map map = (Map)succesResponse;
                Map map1 = (Map)map.get("payload");
                Map map2 = (Map)map1.get("std_data");
                Map map3 = (Map)map2.get("execution");
                Map map4 = (Map) map2.get("parameter");

                if("0".equals(map3.get("code"))){
                    result="SUCCESS";
                }else{
                    System.out.println(map4);

                    //result="ERROR";
                    JSONArray jsonArray = new JSONArray(map4.get("pricing_list_master"));
                    for( int i=0;i<jsonArray.size();i++){
                        JSONObject infog = new JSONObject((Map<String, Object>) jsonArray.get(i));
                        Map mapg = (Map) infog;
                        result= mapg.get("execute_description").toString();//1
                    }

                }


            } catch (Exception e) {}
        }else{
            result="NOTNEED";
        }
        System.out.println(result);


    }
}
